var express = require('express');
var router = express.Router();
const fs = require('fs');
const mongodb= require('../components/mongodb');

/* GET users listing. */
router.get('/', function(req, res) {
 
	var rawdata = fs.readFileSync('0524_testing_cases.json');
	var _testing_cases_dataset_ = JSON.parse(rawdata);
	var testing_cases = [];
	var count = 0;
	console.log(_testing_cases_dataset_[0]);
	_testing_cases_dataset_.forEach(function(item){
		var id = item['住院流水号'];
		var userId = item['患者编号'];
		var name = item['姓名'];
		var gender = item['性别'];
		var age = item['年龄'];
		 //status
		var mstatus = {    //患者状态 Object
			healing:false, //治愈 Boolean
			better:false,    //好转 Boolean
			worse:false,    //恶化 Boolean
			dead:false    //死亡Boolean
		}
		 
		var status_tmp_array=['治愈','好转','恶化','死亡'];
		var status_bool_array = ['healing','better','worse','dead'];
		status_tmp_array.forEach(function(value,index){
			if(item[value]=='有'){
				mstatus[status_bool_array[index]]=true;
			}
		})
			 
		 
		//diagnose
		var diagnose = item['诊断'];
			
		// //MLS
		var MLS_tmp_array = item['检验'];
		// var MLS_tmp_array = MLS_str.split(/[【】]/);
		var MLS = [];
		for(prop in MLS_tmp_array){
			var _status = MLS_tmp_array[prop];
			var positive = false;
			 if(_status=='阳性'||_status=='阴性'){
				if(_status=='阳性'){
					positive = true;
				} else{
					positive = false;
				}
				MLS.push({
					name:prop,
					positive:positive
				})
			 } 			 
			 }
				 
		 //medicines
		var medicine_tmp_array = item['用药'];
		var medicines = [];
		for(mitem in medicine_tmp_array){
			medicines.push({
				  name:mitem,
				  dosis:medicine_tmp_array[mitem][0],
				  unit:medicine_tmp_array[mitem][1]
			  })
		}

		var line = {
		  id:id,
		  userId:userId,
		  gender:gender,
		  age:age,
		  name:name,
		  diagnose:diagnose,
		  MLS:MLS,
		  medicines:medicines,
		  status:mstatus
		}
		  
		testing_cases.push(line);
	})
	
	console.log(testing_cases.slice(0,3));
	//const result = mongodb.insert("case",testing_cases);
	res.send(result);
});

module.exports = router;
